﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {

    public class AssetUrlManager {

        private static var m_instance:AssetUrlManager;

        protected var m_assetPrefix:String;
        protected var m_hashedPrefix:String;
        protected var m_index:Object;
        private var m_packIndex:Object;
        protected var m_domains:int;

        public function AssetUrlManager(_arg1:String, _arg2:String, _arg3:String, _arg4:String):void{
            var _local5:Array;
            var _local6:String;
            var _local7:int;
            var _local8:Array;
            super();
            GlobalEngine.assert((AssetUrlManager.m_instance == null), "Multiple AssetUrlManagers instantiated");
            AssetUrlManager.m_instance = this;
            this.m_assetPrefix = _arg1;
            this.m_hashedPrefix = _arg2;
            if (_arg3 != null){
                this.m_index = new Object();
                _local5 = _arg3.replace(/(^\s*|\s*$)/, "").split("\n");
                _local7 = 0;
                while (_local7 < _local5.length) {
                    _local6 = _local5[_local7];
                    _local8 = _local6.split(":", 2);
                    if ((((_local8.length == 2)) && (((_local8[1] as String).length > 0)))){
                        this.m_index[_local8[1]] = _local8[0];
                    };
                    _local7++;
                };
            };
            this.m_domains = 1;
            if (_arg4 != null){
                this.m_packIndex = new Object();
                _local5 = _arg4.replace(/(^\s*|\s*$)/, "").split("\n");
                _local7 = 0;
                while (_local7 < _local5.length) {
                    _local6 = _local5[_local7];
                    _local8 = _local6.split(":", 2);
                    if ((((_local8.length == 2)) && (((_local8[1] as String).length > 0)))){
                        this.m_packIndex[_local8[1]] = _local8[0];
                    };
                    _local7++;
                };
            };
        }
        public static function get instance():AssetUrlManager{
            GlobalEngine.assert(!((AssetUrlManager.m_instance == null)), "AssetUrlManager not instantiated");
            return (AssetUrlManager.m_instance);
        }
        public static function get initialized():Boolean{
            return (!((AssetUrlManager.m_instance == null)));
        }
        public static function retire():AssetUrlManager{
            var _local1:AssetUrlManager = AssetUrlManager.m_instance;
            AssetUrlManager.m_instance = null;
            return (_local1);
        }

        public function setAssetDomains(_arg1:int):void{
            this.m_domains = _arg1;
        }
        public function lookUpUrl(_arg1:String):String{
            var _local2:String;
            var _local3:Array;
            var _local4:String;
            var _local5:int;
            var _local6:String;
            var _local7:int;
            if (((!((this.m_index == null))) && ((_arg1.indexOf(this.m_assetPrefix) == 0)))){
                _local2 = _arg1.substr(this.m_assetPrefix.length);
                if (this.m_index[_local2]){
                    _local3 = _arg1.split(".");
                    _local4 = ("." + _local3[(_local3.length - 1)]);
                    if (this.m_domains <= 1){
                        return (((this.m_hashedPrefix + this.m_index[_local2]) + _local4));
                    };
                    _local5 = (parseInt(this.m_index[_local2].toString().substr(0, 4), 16) % this.m_domains);
                    _local6 = "://";
                    _local7 = (this.m_hashedPrefix.indexOf(_local6) + _local6.length);
                    _arg1 = ((((((this.m_hashedPrefix.substr(0, _local7) + "a") + _local5) + ".") + this.m_hashedPrefix.substr(_local7)) + this.m_index[_local2]) + _local4);
                    return (_arg1);
                };
                return (_arg1);
            };
            return (_arg1);
        }
        public function isPackedResource(_arg1:String):Boolean{
            return (!((this.lookupPackedResourceUrl(_arg1) == null)));
        }
        public function lookupPackedResourceUrl(_arg1:String):String{
            var _local2:String;
            if (((!((this.m_packIndex == null))) && ((_arg1.indexOf(this.m_assetPrefix) == 0)))){
                _local2 = _arg1.substr(this.m_assetPrefix.length);
                if (this.m_packIndex[_local2]){
                    return (((this.m_hashedPrefix + this.m_packIndex[_local2]) + ".zip"));
                };
            };
            return (null);
        }
        public function isHashedAsset(_arg1:String):Boolean{
            if (this.isPackedResource(_arg1)){
                return (true);
            };
            if (this.lookUpUrl(_arg1) != _arg1){
                return (true);
            };
            return (false);
        }

    }
}//package Engine.Managers 
